package com.hzit.manager.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.hzit.manager.service.IBrandService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 品牌管理Controller
 * 
 * @author wf
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController
{
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Brand brand)
    {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }
    // 查询所有品牌
    @GetMapping("/findAll")
    public R<List<Brand>> findAll(@RequestHeader(value="test",required = false) String test){
        System.out.println("test = " + test);
        List<Brand> brands = brandService.selectBrandList(null);
        return R.ok(brands);
    }
    //1. 实现分布式锁的第一种方式
    @GetMapping("/findAll2")
    public R<List<Brand>> findAll2() throws InterruptedException {
        List<Brand> brands = brandService.findAll2();
        return R.ok(brands);
    }
    //2. 实现分布式锁的第二种方式
    @GetMapping("/findAll3")
    public R<List<Brand>> findAll3() throws InterruptedException {
        List<Brand> brands = brandService.findAll3();
        return R.ok(brands);
    }
    /**
     * 导出品牌管理列表
     */
        @Log(title = "品牌管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Brand brand)
    {
        List<Brand> list = brandService.selectBrandList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        util.exportExcel(response, list, "品牌管理数据");
    }

    @PostMapping("/save")
    public R save(){
         brandService.saveBrand();
         return R.ok();
    }
    /**
     * 获取品牌管理详细信息
     */
        @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(brandService.selectBrandById(id));
    }

    /**
     * 新增品牌管理
     */
        @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand)
    {
        return toAjax(brandService.insertBrand(brand));
    }

    /**
     * 修改品牌管理
     */
        @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand)
    {
        return toAjax(brandService.updateBrand(brand));
    }

    /**
     * 删除品牌管理
     */
        @Log(title = "品牌管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(brandService.deleteBrandByIds(ids));
    }
}
